package com.sgb.kjwl.view.ui.activity.packers;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityWorkPersonnelBinding;
import com.sgb.kjwl.di.packers.DaggerPackersComponent;
import com.sgb.kjwl.di.packers.PackersModule;
import com.sgb.kjwl.model.entity.self.SelfTeamMemberEntity;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.activity.self.packers.SelfTeamMemberEditActivity;
import com.sgb.kjwl.view.ui.adapter.packers.WorkPersonnelAdapter;
import com.sgb.kjwl.viewmodel.packers.WorkPersonnelModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.util.MToast;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import javax.inject.Inject;

/**
 * Description:选择工队人员
 *
 * @author F_Q
 * @date 2021/3/15
 */
public class WorkPersonnelActivity  extends AppActivity {

    private ActivityWorkPersonnelBinding mBind;
    @Inject
    WorkPersonnelModel viewModel;
    private WorkPersonnelAdapter workpersonneladapter;
    private boolean isLoadMore;
    private final int REQUEST_ADD_CODE = 104;
    private int page;

    private static final String TEAM_NO="teamNo";
    public static void start(Context context,String teamNo) {
        Intent intent = new Intent();
        intent.putExtra(TEAM_NO,teamNo);
        intent.setClass(context, WorkPersonnelActivity.class);
        context.startActivity(intent);
    }
    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerPackersComponent.builder().appComponent(appComponent).packersModule(new PackersModule(this)).build().inject(this);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActionBar.setOnBackClickListener(v -> viewModel.teamParticulars(getIntent().getStringExtra(TEAM_NO)));
    }

    @Override
    protected void initView() {
        mBind = DataBindingUtil.setContentView(this, R.layout.activity_work_personnel);
        mBind.setViewModel(viewModel);
        mBind.results.setOnClickListener(this);
        mBind.addWorkStaff.setOnClickListener(this);
        mBind.allPersonnelSelected.setOnClickListener(this);
        adapterData();
    }

    /**
     * LRecyclerView-adapter初始化
     */
    private void adapterData() {
        workpersonneladapter = new WorkPersonnelAdapter(this,null);
        LRecyclerViewAdapter mLRecyclerViewAdapter = new LRecyclerViewAdapter(workpersonneladapter);
        mBind.lrv.setLayoutManager(new LinearLayoutManager(this));
        mBind.lrv.setAdapter(mLRecyclerViewAdapter);
        mBind.lrv.setOnRefreshListener(() -> {
            //下拉刷新
            isLoadMore = false;
            page=1;
            viewModel.refreshAndLoadMore(page,getIntent().getStringExtra(TEAM_NO));
        });
        mBind.lrv.setOnLoadMoreListener(() -> {
            //加载更多
            isLoadMore = true;
            page++;
            viewModel.refreshAndLoadMore(page,getIntent().getStringExtra(TEAM_NO));
        });
        //点击Checkbox更新数据
        workpersonneladapter.setCheckedBox(new WorkPersonnelAdapter.OnCheckedChangeCheckBox() {
            @Override
            public void selectData() {
                setButtonTextView();
            }

            @Override
            public void detailsData(String no) {
                viewModel.getH5Url(no);
            }
        });
    }

    @Override
    protected void init() {
        //首次接口数据请求
        isLoadMore = false;
        page=1;
       viewModel.refreshAndLoadMore(page,getIntent().getStringExtra(TEAM_NO));
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.results) {
            SelfTeamMemberEditActivity.start(this, null, REQUEST_ADD_CODE);
        } else if (id == R.id.add_work_staff) {
            addWorkStaff();
        } else if (id == R.id.all_personnel_selected) {
            if (mBind.allPersonnelSelected.isChecked()) {
                allDataList();
            } else {
                cancelSelectedList();
            }
        }
    }

    private void addWorkStaff() {
        List<String> staffListNo = new ArrayList<>();
        for (SelfTeamMemberEntity data : workpersonneladapter.getDatas()) {
            if (data.isLooks()) {
                staffListNo.add(data.getNo());
            }
        }
        // 类型 1 人员；2 设备；3 业绩； 4 工队相册；5 施工风采
        viewModel.setAddRelation(staffListNo,getIntent().getStringExtra(TEAM_NO),"1");
    }

    /**
     * 全选处理
     */
    private void allDataList() {
        for (SelfTeamMemberEntity data : workpersonneladapter.getDatas()) {
            data.setLooks(true);
        }
        workpersonneladapter.notifyDataSetChanged();
        setButtonTextView();
    }

    /**
     * 全部取消处理
     */
    private void cancelSelectedList() {
        for (SelfTeamMemberEntity data : workpersonneladapter.getDatas()) {
            data.setLooks(false);
        }
        workpersonneladapter.notifyDataSetChanged();
        setButtonTextView();
    }


    /**
     * 返回数据处理
     * @param data 列表的数据
     */
    public void setWorkPersonnelData(List<SelfTeamMemberEntity> data ) {
        if (data ==null){
            return;
        }
        mBind.lrv.refreshComplete(page);
        mBind.lrv.setPullRefreshEnabled(true);
        if (isLoadMore) {
            if (data.size() > 0) {
                workpersonneladapter.addDatas(data);
            } else {
                MToast.showToast(getApplicationContext(), "没有数据信息啦！");
            }
        } else {
            if (data.size() > 0) {
                mBind.listView.setVisibility(View.VISIBLE);
                mBind.emptyView.setVisibility(View.GONE);
                workpersonneladapter.setDatas(data);
            } else {
                mBind.listView.setVisibility(View.GONE);
                mBind.emptyView.setVisibility(View.VISIBLE);
                workpersonneladapter.setDatas(null);
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if ( requestCode == REQUEST_ADD_CODE) {
                //重新获取工队业绩列表信息
                init();
            }
        }
    }

    public void showEmpty() {
        mBind.listView.setVisibility(View.GONE);
        mBind.emptyView.setVisibility(View.VISIBLE);
    }

    public void setUrlView(String url) {
        WebViewX5HostActivity.start(WorkPersonnelActivity.this, url);
        finish();
    }

    public  void  setButtonTextView(){
        int selectedNum = 0;
        for (SelfTeamMemberEntity data : workpersonneladapter.getDatas()) {
            if (data.isLooks()){
                selectedNum++;
            }
        }
        mBind.addWorkStaff.setText(String.format(Locale.CHINA,"添加工队设备（%d）",selectedNum));
    }
}
